package com.rui.study.algorithm.P_单链表反转;


import com.rui.study.algorithm.ListNode;

public class Solution {

    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null)
            return head;
        recursive(head);
        ListNode r = head.next;
        head.next = null;
        return r;
    }

    private ListNode recursive(ListNode node) {
        if (node.next != null && node.next.next == null) {
            node.next.next = node;
        }
        else {
            ListNode nextNode = recursive(node.next);
            node.next = nextNode.next;
            nextNode.next = node;
        }
        return node;
    }
}
